﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BookStore.Solution.BLL;
using BookStore.Solution.Utilities;
using BookStore.Solution.Entities;

namespace BookStore.Solution.Presentation
{
    public partial class ucSystemLog : UserControl
    {
        DataTable MySysytemLogtable;
        SystemLogManager manager = new SystemLogManager();
        private void LoadData(DataTable table)
        {
            dgvListOfSystemLog.AutoGenerateColumns = false;
            dgvListOfSystemLog.DataSource = table;
            for (int i = 0; i < dgvListOfSystemLog.Columns.Count; i++)
            {
                dgvListOfSystemLog.Columns[i].DataPropertyName = dgvListOfSystemLog.Columns[i].Name;
            }
        }

        public ucSystemLog()
        {
            InitializeComponent();
        }

        private void ucSystemLog_Load(object sender, EventArgs e)
        {
            MySysytemLogtable = manager.GetAll();
            LoadData(MySysytemLogtable);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn xóa nhật ký này không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                if (dgvListOfSystemLog.SelectedRows.Count > 0)
                {
                    SystemLog a = new SystemLog();
                    a.SystemLogID = DataUtil.ToInt(dgvListOfSystemLog.SelectedRows[0].Cells[0].Value);
                    a.EmployeeID = DataUtil.ToString(dgvListOfSystemLog.SelectedRows[0].Cells[1].Value);
                    a.Time = DataUtil.ToDate(dgvListOfSystemLog.SelectedRows[0].Cells[2].Value);
                    a.Action = DataUtil.ToString(dgvListOfSystemLog.SelectedRows[0].Cells[3].Value);
                    string resultText = manager.Delete(a);
                    if (resultText == "Xóa thành công")
                    {
                        MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        MySysytemLogtable.Rows.RemoveAt(dgvListOfSystemLog.SelectedRows[0].Index);
                        LoadData(MySysytemLogtable);
                    }
                    else
                        MessageBox.Show(resultText, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                    MessageBox.Show("Phải chọn một nhật ký để xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            btnSearch.Expanded = true;
        }

        private void biSearchByTime_Click(object sender, EventArgs e)
        {
            frmSearchSystemLog frm = new frmSearchSystemLog(SearchSystemLogType.Time);
            frm.ShowDialog();
            if (frm.SearchResult != null)
                LoadData(frm.SearchResult);
        }

        private void biSearchByEmployee_Click(object sender, EventArgs e)
        {
            frmSearchSystemLog frm = new frmSearchSystemLog(SearchSystemLogType.Employee);
            frm.ShowDialog();
            if (frm.SearchResult != null)
                LoadData(frm.SearchResult);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            MySysytemLogtable = manager.GetAll();
            LoadData(MySysytemLogtable);
        }

        private void dgvListOfSystemLog_DoubleClick(object sender, EventArgs e)
        {
            if (dgvListOfSystemLog.SelectedRows.Count > 0)
            {
                SystemLog entity = new SystemLog();
                entity.SystemLogID = DataUtil.ToInt(dgvListOfSystemLog.SelectedRows[0].Cells[0].Value);
                entity.EmployeeID = DataUtil.ToString(dgvListOfSystemLog.SelectedRows[0].Cells[1].Value);
                entity.Time = DataUtil.ToDate(dgvListOfSystemLog.SelectedRows[0].Cells[2].Value);
                entity.Action = DataUtil.ToString(dgvListOfSystemLog.SelectedRows[0].Cells[3].Value);
                frmSystemLog frm = new frmSystemLog(entity);
                frm.ShowDialog();
            }
        }
    }
}
